package com.lqzs.businessmanager.utils

import android.os.Build
import androidx.annotation.RequiresApi
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale
import java.util.regex.Matcher
import java.util.regex.Pattern


object StringUtils {

    /**
     * 从数据中获取日期
     */
    fun getTime(string: String): String? {
        val regex = """\d{4}-\d{2}-\d{2}""".toRegex()
        val matchResult = regex.find(string)
        matchResult?.value?.let {
            return it
        }
        return null
    }

    /**
     * 检验手机号
     */
    fun isValidPhoneNumber(phoneNumber: String): Boolean {
        val regex = "^1(3\\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$"
        val pattern: Pattern = Pattern.compile(regex)
        val matcher: Matcher = pattern.matcher(phoneNumber)
        return matcher.matches()
    }


    /**
     * 比较字符串日期大小
     */
    @RequiresApi(Build.VERSION_CODES.O)
    fun compareDateStrings(dateString1: String, dateString2: String): Int {
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
        val date1 = LocalDate.parse(dateString1, formatter)
        val date2 = LocalDate.parse(dateString2, formatter)

        return date1.compareTo(date2)
    }

    fun getName(string: String): String? {
        string?.let {
            val index = string.indexOf("\n")
            if (index > -1) {
                return string.substring(0, index)
            }
            return null
        }
        return null
    }

    fun getTime(timestamp: Long): String {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        return dateFormat.format(timestamp * 1000)
    }

    fun getTime2(timestamp: Long): String {
        val dateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault())
        return dateFormat.format(timestamp * 1000)
    }
}